home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / modlib_s.lha / modlib_src / $dcg.P < prev    next >
Text File  |  1990-04-12  |  4KB  |  143 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $dcg.P */
  25.  
  26.  
  27. % File: $dcg.P
  28. % Author: Saumya K. Debray
  29. % Date: March 15, 1988
  30. %
  31. % This is a straightforward translator for DCGs.
  32. %
  33. % Bugs: pushback lists in the heads of rules are not handled.  This
  34. % is a rarely used feature, and I didn't see it worth my while to
  35. % complicate life for this.
  36.  
  37. $dcg_export([$dcg/2,$phrase/2,$phrase/3]).
  38.  
  39. $dcg_use($meta,[functor/3,_,_]).
  40. $dcg_use($bio, [$writename/1,_,_,$nl/0,_,_,_,_,_,_,_,_,_,_,_]).
  41.  
  42.  
  43. $dcg(Rule, (OutHd :- OutBody)) :-
  44.      nonvar(Rule),
  45.      Rule = (Lhs --> Rhs),
  46.      (functor(Lhs,',',2) ->
  47.           ($writename('*** Pushback lists in DCG Rule heads not implemented!'),
  48.        $nl,
  49.        fail
  50.       ) ;
  51.           $add_io_args(Lhs, In, Out, OutHd)
  52.      ),
  53.      $dcg_body(Rhs, In, Out, OutBody).
  54.  
  55. $add_io_args(Term, In, Out, NewTerm) :-
  56.      functor(Term,P,Arity),
  57.      NewArity is Arity + 2,
  58.      N1 is Arity + 1,
  59.      functor(NewTerm,P,NewArity),
  60.      arg(N1,NewTerm,In),
  61.      arg(NewArity,NewTerm,Out),
  62.      $dcg_fillin_args(Arity,Term,NewTerm).
  63.  
  64. $dcg_fillin_args(N,T1,T2) :-
  65.      N =:= 0 ->
  66.          true ;
  67.          (arg(N,T1,Arg), arg(N,T2,Arg),
  68.       N1 is N - 1,
  69.       $dcg_fillin_args(N1,T1,T2)
  70.      ).
  71.  
  72. $dcg_body(Rhs,In,Out,Goal) :-
  73.      var(Rhs) ->
  74.           $dcg_body_var(Rhs,In,Out,Goal) ;
  75.       $dcg_body_nonvar(Rhs,In,Out,Goal).
  76.  
  77. $dcg_body_var(Rhs,In,Out,Goal) :-
  78.      In = Out,
  79.      Goal = ($writename('*** Error: variable in RHS of DCG Rule!'),
  80.          $nl,fail),
  81.      $writename('*** Error: variable in RHS of DCG Rule!'),
  82.      $nl.
  83.  
  84. $dcg_body_nonvar(','(G1,G2),In,Out,','(T1,T2)) :-
  85.      !,
  86.      $dcg_body(G1,In,Mid,T1),
  87.      $dcg_body(G2,Mid,Out,T2).
  88. $dcg_body_nonvar(';'(G1,G2),In,Out,';'(T1,T2)) :-
  89.      !,
  90.      $dcg_body(G1,In,Out,T1),
  91.      $dcg_body(G2,In,Out,T2).
  92. $dcg_body_nonvar('->'(G1,G2),In,Out,'->'(T1,T2)) :-
  93.      !,
  94.      $dcg_body(G1,In,Mid,T1),
  95.      $dcg_body(G2,Mid,Out,T2).
  96. $dcg_body_nonvar(not(G),In,Out,not(T)) :-
  97.      !,
  98.      $writename('*** Warning: not/1 in DCG rule may not be sound!'),
  99.      $nl,
  100.      $dcg_body(G,In,Out,T).
  101. $dcg_body_nonvar('\+'(G),In,Out,not(T)) :-
  102.      !,
  103.      $writename('*** Warning: \+ in DCG rule may not be sound!'),
  104.      $nl,
  105.      $dcg_body(G,In,Out,T).
  106. $dcg_body_nonvar('{}'(G),In,Out,(In = Out, call(G))) :- !.
  107. $dcg_body_nonvar('!',In,Out ,(In = Out, '!')) :- !.
  108. $dcg_body_nonvar([],In,Out,(In = Out)) :- !.
  109. $dcg_body_nonvar(L,In,Out,G) :-
  110.      (L = [_|_], $dcg_list(L)) ->
  111.           $dcg_terminal(L,In,Out,G) ;
  112.       $add_io_args(L,In,Out,G).    /* nonterminal */
  113.  
  114. $dcg_list([]).
  115. $dcg_list(L) :-
  116.      nonvar(L) ->
  117.           (L = [_|L1], $dcg_list(L1)) ;
  118.           ($writename('*** Error: variable within a list of terminals in DCG rule'),
  119.        $nl,
  120.        fail
  121.       ).
  122.  
  123. $dcg_terminal([],In,Out,(In = Out)).
  124. $dcg_terminal([H|L],In,Out,','('C'(In,H,Mid),G)) :-
  125.      $dcg_terminal(L,Mid,Out,G).
  126.  
  127. 'C'([X|S],X,S).
  128.  
  129. $phrase(P,L) :-
  130.      nonvar(P) ->
  131.           $phrase(P,L,[]) ;
  132.           ($writename('*** First argument to phrase/2 must be nonvariable!'),
  133.        $nl,
  134.        fail
  135.       ).
  136.  
  137. $phrase(P,In,Out) :-
  138.      $dcg_body_nonvar(P,In,Out,Goal),
  139.      call(Goal).     
  140.  
  141. /* ------------------------- end $dcg.P ------------------------- */
  142.  
  143.